{% extends 'admin/common/base.html' %}
{% block css %}
{{super()}}
<link href="{{url_for('admin.static',filename='ajax/libs/select2/select2.min.css')}}" rel="stylesheet" />
<link href="{{url_for('admin.static',filename='ajax/libs/select2/select2-bootstrap.css')}}" rel="stylesheet" />
{% endblock %}
{% block body %}

<body class="white-bg">
    <div class="main-content">
        <form id="form-user-add" class="form-horizontal">
            <h4 class="form-header h4">基本信息</h4>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">登录账号：</label>
                        <div class="col-sm-8">
                            <input id="username" name="username" placeholder="请输入登录账号" class="form-control" type="text"
                                maxlength="30" required>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">登录密码：</label>
                        <div class="col-sm-8">
                            <div class="input-group">
                                <input id="password" name="password" placeholder="请输入登录密码" class="form-control"
                                    type="password" value="123456" required>
                                <span class="input-group-addon" title="登录密码,鼠标按下显示密码"
                                    onmousedown="$('#password').attr('type','text')"
                                    onmouseup="$('#password').attr('type','password')"><i class="fa fa-key"></i></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">用户名称：</label>
                        <div class="col-sm-8">
                            <input name="name" placeholder="请输入用户名称" class="form-control" type="text" maxlength="30"
                                required>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">手机号码：</label>
                        <div class="col-sm-8">
                            <div class="input-group">
                                <input id="mobile" name="mobile" placeholder="请输入手机号码" class="form-control" type="text"
                                    maxlength="11">
                                <span class="input-group-addon"><i class="fa fa-mobile"></i></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">邮箱：</label>
                        <div class="col-sm-8">
                            <div class="input-group">
                                <input id="email" name="email" class="form-control email" type="text" maxlength="50"
                                    placeholder="请输入邮箱">
                                <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">用户类型：</label>
                        <div class="col-sm-8">
                            <select id="user_type" name="user_type" class="form-control m-b">
                                {% for dd in get_dict_data('sys_user_type')%}
                                <option value="{{dd.value}}" {{'selected' if dd.is_default=='Y' else '' }}>{{dd.label}}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">用户性别：</label>
                        <div class="col-sm-8">
                            <select name="sex" class="form-control m-b">
                                <option value="男" selected>男</option>
                                <option value="女">女</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">用户状态：</label>
                        <div class="col-sm-8">
                            <label class="toggle-switch switch-solid">
                                <input type="checkbox" id="status" checked>
                                <span></span>
                            </label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">归属部门：</label>
                        <div class="col-sm-8">
                            <div class="input-group">
                                <input type="hidden" id="org_id" name="org_id" value="">
                                <input class="form-control" type="text" onclick="selectDeptTree()" id="org_name"
                                    value="">
                                <span class="input-group-addon"><i class="fa fa-search"></i></span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">

                </div>

                <div class="row">
                    <div class="col-sm-12">
                        <div class="form-group">
                            <label class="col-xs-2 control-label">角色：</label>
                            <div class="col-xs-10">
                                {% for role in roles %}
                                <label class="radio  check-box">
                                    <input name="role_id" type="radio" value="{{role.id}}">{{role.name}}</input>
                                </label>
                                {% endfor %}
                            </div>
                        </div>
                    </div>
                </div>
                <h4 class="form-header h4">其他信息</h4>
                <div class="row">
                    <div class="col-sm-12">
                        <div class="form-group">
                            <label class="col-xs-2 control-label">备注：</label>
                            <div class="col-xs-10">
                                <textarea name="remark" maxlength="500" class="form-control" rows="3"></textarea>
                            </div>
                        </div>
                    </div>
                </div>
        </form>
    </div>

    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保
                存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭
            </button>
        </div>
    </div>
</body>
{% endblock %}
{% block js %}
{{ super() }}
<script src="{{url_for('admin.static',filename='ajax/libs/select2/select2.min.js')}}"></script>
<script>
    var ctx = '';
    var prefix = ctx + "system/user";

    $("#form-user-add").validate({
        onkeyup: false,
        rules: {
            username: {
                minlength: 2,
                maxlength: 20,
                remote: {
                    url: "{{url_for('admin.sys.user.check_username_unique')}}",
                    type: "post",
                    dataType: "json",
                    data: {
                        "username": function () {
                            return $.common.trim($("#username").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            password: {
                minlength: 5,
                maxlength: 20
            },
            // mobile: {
            //     isPhone: true,
            //     remote: {
            //         url: "{{url_for('admin.sys.user.check_mobile_unique')}}",
            //         type: "post",
            //         dataType: "json",
            //         data: {
            //             "mobile": function () {
            //                 return $.common.trim($("#mobile").val());
            //             }
            //         },
            //         dataFilter: function (data, type) {
            //             return $.validate.unique(data);
            //         }
            //     }
            // },

        },
        messages: {
            "username": {
                remote: "用户已经存在"
            },
            "mobile": {
                remote: "手机号码已经存在"
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        var chrtype = 0;
        var password = $("#password").val();
        if ($.validate.form() && checkpwd(chrtype, password)) {
            var data = $("#form-user-add").serializeArray();
            var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
            data.push({ "name": "status", "value": status });
            $.operate.saveTab("{{url_for('admin.sys.user.add')}}", data);
        }
    }
    /* 用户管理-修改-选择部门树 */
    function selectDeptTree() {
        var deptId = $.common.isEmpty($("#org_id").val()) ? "1" : $("#org_id").val();
        var url = "{{url_for('admin.sys.org.select_tree')}}?id=" + deptId;
        var options = {
            title: '选择归属部门',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var body = $.modal.getChildFrame(index);
        $("#org_id").val(body.find('#treeId').val());
        $("#org_name").val(body.find('#treeName').val());
        $.modal.close(index);
    }
</script>
{% endblock %}